卷积3-深度可分离卷积Separable Convolutions

https://blog.csdn.net/Spratumn/article/details/103873308

TODO:与卷积1-分组卷积的区别 https://zhuanlan.zhihu.com/p/226448051

将一个卷积操作分为两次完成,可以在只降低1%性能的前提下,实现很大的速度提升。Segformer

原有需要参数:$$N_1=K\times K\times C_{in}\times C_{out}$$
现在需要参数:$$N_1=K\times K\times C_{in} + 1\times 1\times C_{in}\times C_{out}$$

from torch import nn
from torchsummary import summary


class SeparableConv2d(nn.Module):
    def __init__(self,in_channels,out_channels
                ,kernel_size=3,stride=1
                ,padding=0,dilation=1):
        super(SeparableConv2d, self).__init__()
        # groups=in_channels  every filter match one feature map 
        self.deepwise_conv = nn.Conv2d(in_channels,in_channels
                            ,kernel_size=kernel_size
                            ,stride=stride
                            ,padding=padding
                            ,dilation=dilation
                            ,groups=in_channels 
                            ,bias=False)
        self.pointwise_conv = nn.Conv2d(in_channels,out_channels
                            ,kernel_size=1
                            ,bias=False)
    def forward(self,x):
        out = self.pointwise_conv(self.deepwise_conv(x))
        return out

if __name__ == '__main__':
    sep_conv = SeparableConv2d(3,8,kernel_size=7)
    summary(sep_conv,(3,10,10))